import { useState } from 'react'
import { getProductTemplateList } from '@/api/goods'
export interface IDataTemplateType {
  productTemplateName: string
  productTemplateId: string
}
const useSearchTemplate = () => {
  let TemplateTimeout: ITimeoutType
  const [TemplateOptions, setTemplateOptions] = useState<IOptionItem[]>([])
  const handleSearchTemplate = (newValue = '') => {
    if (TemplateTimeout) {
      clearTimeout(TemplateTimeout)
      TemplateTimeout = null
    }
    const getOptions = async () => {
      const res = await getProductTemplateList<IDataTemplateType>({
        keyword: newValue,
      })
      if (res.code === 200) {
        const data =
          res?.data?.list?.map((item) => {
            return {
              label: item.productTemplateName,
              value: item.productTemplateId,
            }
          }) || []
        setTemplateOptions(data)
      }
    }
    TemplateTimeout = setTimeout(getOptions, 500)
  }
  return { TemplateOptions, handleSearchTemplate, setTemplateOptions }
}

export default useSearchTemplate
